import 'package:flutter/cupertino.dart';
import 'package:oktoast/oktoast.dart';
import 'package:flutter/material.dart';

const Color _bgColor = Colors.black87;
const double _radius = 3.0;

class KYToast{

  static ToastView? preToast;

  //聊天界面列表接口错误
  static showNetWorkError(BuildContext context,String msg){
    preToast?.dismiss();
    preToast = null;

    var overlayState = Overlay.of(context)!;
    var controllerShowAnim = new AnimationController(
      vsync: overlayState,
      duration: Duration(milliseconds: 250),
    );
    var controllerShowOffset = new AnimationController(
      vsync: overlayState,
      duration: Duration(milliseconds: 350),
    );
    var controllerHide = new AnimationController(
      vsync: overlayState,
      duration: Duration(milliseconds: 250),
    );
    var opacityAnim1 =
    new Tween(begin: 0.0, end: 1.0).animate(controllerShowAnim);
    var controllerCurvedShowOffset = new CurvedAnimation(
        parent: controllerShowOffset, curve: _BounceOutCurve._());
    var offsetAnim =
    new Tween(begin: 30.0, end: 0.0).animate(controllerCurvedShowOffset);
    var opacityAnim2 = new Tween(begin: 1.0, end: 0.0).animate(controllerHide);

    OverlayEntry overlayEntry;
    overlayEntry = new OverlayEntry(builder: (context) {
      return ToastWidget(
        opacityAnim1: opacityAnim1,
        opacityAnim2: opacityAnim2,
        offsetAnim: offsetAnim,
        child: buildToastLayout(msg),
      );
    });
    var toastView = ToastView();
    toastView.overlayEntry = overlayEntry;
    toastView.controllerShowAnim = controllerShowAnim;
    toastView.controllerShowOffset = controllerShowOffset;
    toastView.controllerHide = controllerHide;
    toastView.overlayState = overlayState;
    preToast = toastView;
    toastView._show();
  }

  static LayoutBuilder buildToastLayout(String msg) {
    return LayoutBuilder(builder: (context, constraints) {
      return IgnorePointer(
        ignoring: true,
        child: Container(
          child: Material(
            color: Colors.white.withOpacity(0),
            child: Container(
              child: Container(
                child: Text(
                  "${msg}",
                  style: TextStyle(color: Colors.white),
                ),
                decoration: BoxDecoration(
                  color: Colors.black.withOpacity(0.4),
                  borderRadius: BorderRadius.all(
                    Radius.circular(5),
                  ),
                ),
                padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
              ),
              margin: EdgeInsets.only(
                bottom: constraints.biggest.height * 0.1,
                left: constraints.biggest.width * 0.2,
                right: constraints.biggest.width * 0.2,
              ),
            ),
          ),
          alignment: Alignment.bottomCenter,
        ),
      );
    });
  }

  static showText(
      @required String loadingText,
      ){
    _showText(loadingText);
  }
  static showSuccess(
      @required String loadingText,
      ){
    _showSuccess(loadingText);
  }
  static showError(
      @required String loadingText,
      ){
    _showError(loadingText);
  }
  static showInfo(
      @required String loadingText,
      ){
    _showInfo(loadingText);
  }

  static showLoadingText({
    String loadingText = "加载中...",
  }){
    _showLoading(loadingText);
  }

  static hideHUD(){
    dismissAllToast();
  }

}

void _showText(loadingText) {
  var w = Container(
    margin: const EdgeInsets.all(50.0),
    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
    decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(_radius)),
    child: ClipRect(
        child:
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Text(loadingText,
                style: TextStyle(fontSize: 16,), textAlign: TextAlign.center),
          ],
        )
    ),
  );
  showToastWidget(w);
}


void _showLoading(loadingText) {
  var w = Container(
    margin: const EdgeInsets.all(50.0),
    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
    decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(_radius)),
    child: ClipRect(
        child:
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: 40.0,
              height: 40.0,
              margin: EdgeInsets.only(bottom: 8.0),
              padding: EdgeInsets.all(4.0),
              child: CircularProgressIndicator(
                strokeWidth: 3.0,
                valueColor:AlwaysStoppedAnimation<Color>(Colors.white),
              ),
            ),
            Text(loadingText,
                style: TextStyle(fontSize: 16,), textAlign: TextAlign.center),
          ],
        )
    ),
  );
  var bg = Container(
    width: 2000,
    height: 2000,
    color: Colors.transparent,
    child: Center(child: w,),
  );


  showToastWidget(bg,duration:Duration(seconds: 30),handleTouch: true);
}

void _showSuccess(loadingText) {
  var w = Container(
    margin: const EdgeInsets.all(50.0),
    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
    decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(_radius)),
    child: ClipRect(
        child:
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: 40.0,
              height: 40.0,
              margin: EdgeInsets.only(bottom: 8.0),
              padding: EdgeInsets.all(4.0),
              child: Icon(Icons.check_circle_outline, size: 30, color: Colors.white,),
            ),
            Text(loadingText,
                style: TextStyle(fontSize: 16,), textAlign: TextAlign.center),
          ],
        )
    ),
  );
  showToastWidget(w);
}


void _showError(loadingText) {
  var w = Container(
    margin: const EdgeInsets.all(50.0),
    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
    decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(_radius)),
    child: ClipRect(
        child:
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: 40.0,
              height: 40.0,
              margin: EdgeInsets.only(bottom: 8.0),
              padding: EdgeInsets.all(4.0),
              child: Icon(Icons.highlight_off, size: 30, color: Colors.white,),
            ),
            Text(loadingText,
                style: TextStyle(fontSize: 16,), textAlign: TextAlign.center),
          ],
        )
    ),
  );
  showToastWidget(w);
}

void _showInfo(loadingText) {
  var w = Container(
    margin: const EdgeInsets.all(50.0),
    padding: EdgeInsets.symmetric(horizontal: 24.0, vertical: 16.0),
    decoration: BoxDecoration(
        color: _bgColor,
        borderRadius: BorderRadius.circular(_radius)),
    child: ClipRect(
        child:
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Container(
              width: 40.0,
              height: 40.0,
              margin: EdgeInsets.only(bottom: 8.0),
              padding: EdgeInsets.all(4.0),
              child: Icon(Icons.info_outline, size: 30, color: Colors.white,), //info_outline
            ),
            Text(loadingText,
                style: TextStyle(fontSize: 16,), textAlign: TextAlign.center),
          ],
        )
    ),
  );
  showToastWidget(w);
}

class ToastView {
  late OverlayEntry overlayEntry;
  late AnimationController controllerShowAnim;
  late AnimationController controllerShowOffset;
  late AnimationController controllerHide;
  late OverlayState overlayState;
  bool dismissed = false;

  _show() async {
    overlayState.insert(overlayEntry);
    controllerShowAnim.forward();
    controllerShowOffset.forward();
    await Future.delayed(Duration(milliseconds: 2000));
    this.dismiss();
  }

  dismiss() async {
    if (dismissed) {
      return;
    }
    this.dismissed = true;
    controllerHide.forward();
    await Future.delayed(Duration(milliseconds: 250));
    if (overlayEntry != null){
      overlayEntry.remove();
    }
  }
}

class ToastWidget extends StatelessWidget {
  final Widget child;
  final Animation<double> opacityAnim1;
  final Animation<double> opacityAnim2;
  final Animation<double> offsetAnim;

  ToastWidget(
      {required this.child, required this.offsetAnim, required this.opacityAnim1, required this.opacityAnim2});

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: opacityAnim1,
      child: child,
      builder: (context, child_to_build) {
        return Opacity(
          opacity: opacityAnim1.value,
          child: AnimatedBuilder(
            animation: offsetAnim,
            builder: (context, _) {
              return Transform.translate(
                offset: Offset(0, offsetAnim.value),
                child: AnimatedBuilder(
                  animation: opacityAnim2,
                  builder: (context, _) {
                    return Opacity(
                      opacity: opacityAnim2.value,
                      child: child_to_build,
                    );
                  },
                ),
              );
            },
          ),
        );
      },
    );
  }
}

class _BounceOutCurve extends Curve {
  const _BounceOutCurve._();

  @override
  double transform(double t) {
    t -= 1.0;
    return t * t * ((2 + 1) * t + 2) + 1.0;
  }
}